﻿package org.openChart 
{
	import flash.display.Sprite;
	
	/**
	 * ...
	 * @author Chongyuan
	 */
	public class TestMain extends Sprite
	{
		private var minValue_:Number = Number.MAX_VALUE;
		// The maximum value .
		private var maxValue_:Number = Number.MIN_VALUE;
		// The step.
		private var step_:Number = 1;
		
		private var textSize_:Number = 16;
		private var text:String = "你哦啊是万恶哦isfdsafew234897ofjasoihf发射机哦服务哦哦i萨芬";
		private var maxWidth_:Number = 80;
		private var width_:Number;
		private var height_:Number;
		
		public function TestMain() 
		{
			initSize();
			initSize();
			trace(text);
		}
		
		public function initSize():void
		{
			var textWidth:Number = 0;
			var textHeight:Number = 0;
			var textValue:String = "";
			
			if (text)
			{
				for (var i:uint = 0; i < text.length; i++)
				{
					//trace(text.charAt(i), "-", text.charCodeAt(i));
					if ( text.charCodeAt(i) != 10)
					{
						if (text.charCodeAt(i) < 0xFF)
						{
							textWidth += textSize_ / 2;
							if (textWidth > maxWidth_)
							{
								textWidth =  textSize_ / 2;
								textValue += "\n";
								//textHeight += textSize_ * 2;
							}
							textValue += text.charAt(i);
						}
						else
						{
							textWidth += textSize_;
							if (textWidth > maxWidth_)
							{
								textWidth =  textSize_ / 2;
								textValue += "\n";
								//textHeight += textSize_ * 2;
							}
							textValue += text.charAt(i);
						}
					}
				}
			}
			text = textValue;
			width_ = textWidth;
			height_ = textHeight;
		}
		
		public function resize():void
		{
			if (minValue_ > maxValue_)
				throw new Error("The min-value is larger than max-value.");
			var labelCount:Number = (maxValue_ - minValue_) / step_;
			if (labelCount < 3 || labelCount > 10)
			{
				resetStep();
				minValue_ = int(minValue_ / step_) * step_;
				maxValue_ = (int(maxValue_  / step_) + 1) * step_;
			}
		}
		
		public function resetStep():void
		{
			trace(step_);
			var labelCount:Number = (maxValue_ - minValue_) / step_;
			if (labelCount > 10)
			{
				step_ *= 10;
				resetStep();
			}
			else if (labelCount < 1)
			{
				step_ /=  10;
				resetStep();
			}
			
		}
	}
	
}